var api = require('../../../config/api.js')
var app = getApp()
var p = 0,
  c = 0,
  d = 0
Page({

  /**
   * 页面的初始数据
   */
  data: {
    toast1Hidden: true,
    modalHidden: true,
    modalHidden2: false,
    notice_str: '',

    provinceName: [],
    provinceCode: [],
    provinceSelIndex: '',
    cityName: [],
    cityCode: [],
    citySelIndex: '',
    districtName: [],
    districtCode: [],
    districtSelIndex: '',
    showMessage: false,
    messageContent: '',
    showDistpicker: false,
  },

  toast1Change: function (e) {
    this.setData({
      toast1Hidden: true
    });
  },

  //表单提交
  formSubmit: function (e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否确认提交',
      confirmColor: "#43CBC9",
      success: (res) => {
        wx.showLoading({
          title: '加载中',
        })

        var trueName = e.detail.value.trueName
        var mobile = e.detail.value.mobile

        if (res.confirm) {
          console.log('for发生了submit事件，携带数据为：', e.detail.value);

          var reg = /^[0-9a-zA-Z]+$/;
          var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
          var myReg = /^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;


          if (trueName.length == 0) {
            wx.showToast({
              title: "真实姓名不能为空!",
              icon: 'none',
              duration: 2000
            });
            return false;
          } else if (mobile.length != 11) {
            wx.showToast({
              title: "手机号长度不正确!",
              icon: 'none',
              duration: 2000
            });
            return false;
          } else if (!myreg.test(mobile)) {
            wx.showToast({
              title: '手机号格式有误！',
              icon: 'none',
              duration: 1500
            })
            return false;
          } 
          wx.showLoading({
            title: '数据提交中',
          })
            wx.request({
              url: api.CustomerAdd +"?mobile="+mobile+"&trueName="+trueName,
              data: {},
              method: 'GET',
              header: {
                'Accept': 'application/json',
                'ACCESS-TOKEN': wx.getStorageSync('token')
              },

              success: function (res) {
                wx.hideLoading();
                if (res.data.success) {
                  wx.showToast({
                    title: "添加成功！",
                    icon: 'none',
                    duration: 3000
                  });
                } else {
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 3000
                  });
                }

              },
              fail: function () {
                wx.hideLoading();
                wx.showToast({
                  title: "登录账号已存在，请重新输入！",
                  icon: 'none',
                  duration: 3000
                });
              },
            })

        } else if (res.cancel) {
          console.log('用户点击取消')
          wx.hideLoading();
        }
      }
    })
  },


  formReset: function () {
    this.setData({
      modalHidden2: false,
      isAgree: false,
      showDistpicker: false,
      provinceSelIndex: null,
    });
  },

  //弹出确认框  
  modalTap: function (e) {
    this.setData({
      modalHidden: true
    })
  },
  confirm_one: function (e) {
    console.log(e);
    this.setData({
      modalHidden: true,
      toast1Hidden: false,
      notice_str: '提交成功'
    });
  },
  cancel_one: function (e) {
    console.log(e);
    this.setData({
      modalHidden: true,
      toast1Hidden: false,
      notice_str: '取消成功'
    });
  },
  //弹出提示框  
  modalTap2: function (e) {
    this.setData({
      modalHidden2: false
    })
  },
  modalChange2: function (e) {
    this.setData({
      modalHidden2: true
    })
  },

  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  showDistpicker: function () {
    this.setData({
      showDistpicker: true
    })
  },
  distpickerCancel: function () {
    this.setData({
      showDistpicker: false
    })
  },
  distpickerSure: function () {
    this.setData({
      provinceSelIndex: p,
      citySelIndex: c,
      districtSelIndex: d
    })
    this.distpickerCancel()
  },
  showMessage: function (text) {
    var that = this
    that.setData({
      showMessage: true,
      messageContent: text
    })
    setTimeout(function () {
      that.setData({
        showMessage: false,
        messageContent: ''
      })
    }, 3000)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})